// pages/my/stockGoodsSet/stockGoodsSet.js
var app = getApp()
Page({
  data: {
    // showView:false,
    putInCart:false,
    good:{},
    currentBox:[],
    goodType: "2",
    goodTypes:[],
    versionValueIds:'',
    productId:'1',
    ifAddUnderLineGoods:false,
    salePrice:'',
    versName:'',
    ver_value_ids:'',
    isStock:true,
    isAdd:false,
    isInventory:false,
    stockType:'',
    secondId:'',
    isCompany:true,
    inventoryNote:'',
    afterStock:'',
    isOldGoods:false
  },
  onLoad: function (options) {
    if (options.type=='98') {
      this.setData({
        isOldGoods:true
      })
    }else{
      this.setData({
        isOldGoods:false
      })
    }
    if (options.stockType==2) {
      wx.setNavigationBarTitle({
        title:'修改价格'
      })
      this.setData({
        stockType:options.stockType
      })
    }else if (options.stockType==0) {
      wx.setNavigationBarTitle({
        title:'溢缺管理'
      })
      this.setData({
        stockType:options.stockType,
        secondId:options.secondId
      })
    }else{
      wx.setNavigationBarTitle({
        title:''
      })
      this.setData({
        stockType:options.stockType
      })
    }
    if (options.price) {
      this.setData({
        ifAddUnderLineGoods:true,
        salePrice:options.price
      })
    }
    this.setData({
      productId:options.id,
      type:options.type
    })
    var that = this
    app.getMemInfo().then(res=>{
      let agentId = res.data.agentId
      let accountId = res.data.id
      this.setData({
        agentId,
        accountId
      })
      this.getGoodInfo(agentId)
      // this.getGoodVersion()
    })

  },
  //获取商品详情
  getGoodInfo(agentId){
    var that = this
    let data = {
      agentId,
      skuId:this.data.productId
    }
    console.log(data)
    app.request('wx_app/agent/stock/product/detail',data,'GET').then(res=>{
      console.log(res)
      let imgs = []
      imgs.push(res.data.product.imgUrl2,res.data.product.imgUrl3,res.data.product.imgUrl4,res.data.product.imgUrl5)
      let good = {
        name:res.data.product.productName,
        url:res.data.product.mainImgUrl,
        stock:res.data.product.stock,
        skuId:res.data.product.id,
        purchasPriceAvg:res.data.avgPrice==null?0:res.data.avgPrice==null,
        purchasPrice:0,
        purchasNum:'',
        price:res.data.product.price==null?0:res.data.product.price,
        imgs,
        goodAttr:res.data.product.versionValues,
        companyName:res.data.productTag,
        color:res.data.tagColor,
        applyTo:res.data.product.applyTo,
        colorName:res.data.product.colorName
      }

      that.setData({
        good
      })
    })
  },
  //获取商品规格
  getGoodVersion(){
    const that = this
    let data = {
      agentId:this.data.agentId,
      productId:this.data.productId,
      type:this.data.type
    }
    console.log(data)
    app.request('wx_app/mall/product/version/detail',data,'GET').then(res=>{
      console.log(res)
      let attr = res.data.versions
      let goodTypes = []
      attr.forEach(item=>{
        goodTypes.push({
          name:item.versionValue,
          id:item.sku_product_id,
          versionValueIds:item.versionValueIds
        })
      })
      that.setData({
        goodTypes
      })
      //获取默认规格
      let currentBox = []
      let versions = []
      attr.forEach(item=>{
        currentBox.push({
          name:item.versionValue,
          id:item.sku_product_id,
          current:false
        })
      })
      currentBox[0].current=true
      console.log(res.data.version_value_ids)
      that.setData({
        currentBox,
        ver_value_ids:res.data.versions[0].versionValueIds
      })
    })
  },
  chooseAttr(e){
    let name = e.currentTarget.dataset.attrName
    let id = e.currentTarget.dataset.attrId
    let index = e.currentTarget.dataset.index
    let ver_value_ids = e.currentTarget.dataset.verValueIds
    let currentBox = this.data.currentBox
    console.log(e.currentTarget.dataset)
    const that = this
    currentBox.forEach((item,i)=>{
      if (item.id==id) {
        item.current=true
      }else{
        item.current=false
      }
    })
    // let goodType = ''
    // currentBox.forEach(item=>{
    //   goodType += ''+item.attr
    // })
    this.setData({
      currentBox,
      versName:name,
      ver_value_ids
      // goodType
    })
    this.getProductStockInfo()
  },
  //根据规格获取库存及价格
  getProductStockInfo(){
    this.getVersions()
    var that = this
    // let data = {
    //   type:this.data.type,
    //   productId:this.data.productId,
    //   agentId:this.data.agentId,
    //   versionValueIds:this.data.versionValueIds,
    // }
    let data = {
      skuId:this.data.versionValueIds,
      agentId:this.data.agentId
    }
    console.log(data)
    app.request('wx_app/agent/stock/productStockInfo',data,'GET').then(res=>{
      console.log(res)
      if (res.code==0) {
        wx.showToast({
          title:'未查询到规格属性',
          icon:'none'
        })
        // that.setData({
        //   versionValueIds:''
        // })
      }else{   
           
        that.setData({
          ['good.stock']:res.data.stock,
          ['good.price']:res.data.price,
          // versionValueIds:that.data.versionValueIds,
          ['good.purchasPrice']:res.data.stockInPrice,
          // ['good.purchasNum']:res.data.stockInCount
        })
      }
    })
  },
  getVersions(){
    let versions = []
    let verSort = []
    let versionValueIds =''
    this.data.currentBox.forEach(item=>{
      if (item.current) {
        this.setData({
          versionValueIds:item.id,
          // ver_value_ids:item.ver_value_ids
        })
      }
    })
    // versions.forEach(item=>{
    //   if (item!='') {
    //     verSort.push(parseInt(item))
    //   }
    // })
    // verSort.sort().forEach(item=>{
    //   versionValueIds+=item+','
    // })
    // this.setData({
    //   versionValueIds:versionValueIds.substr(0,versionValueIds.length-1)
    // })
  },
  saveGoodSet(){
    this.getVersions()
    if (this.data.ifAddUnderLineGoods) {
      this.savePrice()
    }else{
      this.saveStock()
    }
  },
  savePrice(){
    let data = {
      skuId:this.data.productId,
      agentId:this.data.agentId,
      price:this.data.good.price
    }
    console.log(data)
    app.request('wx_app/agent/stock/saveProductStockSetting',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        console.log(777)
        wx.showToast({
          title: '修改成功',
          icon: 'success'
        })
        this.setData({
          isSetPrice:false
        })
      }else{
        app.errorMsg(res.message+'请联系管理员')
      }
    })
  },
  saveStock(){
    const that = this
    if (this.data.good.purchasNum=='') {
      app.errorMsg('请添加进货数量')
    }else{
      let data = {
        skuId:this.data.productId,
        accountId:this.data.accountId,
        agentId:this.data.agentId,
        price:this.data.good.purchasPrice==null?0:this.data.good.purchasPrice,
        stock:this.data.good.purchasNum
      }
      console.log(data)
      app.request('wx_app/agent/stock/flow/add',data,'GET').then(res=>{
        console.log(res)
        if (res.code==1) {
          wx.showToast({
            title: '添加成功',
            icon: 'success',
            duration: 2000,
            success(){
              that.getGoodInfo(that.data.agentId)
            }
          })
        }else{
          app.errorMsg(res.message)
        }
      })
    }
  },
  changePrice(e){
    this.setData({
      ['good.price']:e.detail.value
    })
  },
  purchasNum(e){
    if (e.detail.value>0) {
      this.setData({
        ['good.purchasNum']:e.detail.value
      })
    }else{
      this.setData({
        ['good.purchasNum']:0
      })
    }
  },
  purchasPrice(e){
    this.setData({
      ['good.purchasPrice']:e.detail.value
    })
  },
  selectGTap:function(){
    this.setData({
      showView: false
    })
  },
  selectMoreGTap:function(){
    this.setData({
      showView: true
    })
  },
  closeBtnView:function(){
    this.setData({
      showView: false
    })
  },
  stockDetails:function(){
    let id = this.data.productId
    let type = this.data.type
    let skuId = this.data.productId
    let verIds = this.data.ver_value_ids
    wx.navigateTo({
      url: '../stockDetails/stockDetails?type='+type+'&skuId='+skuId+'&verIds='+verIds
    })
  },
  editPrice(e){
    this.setData({
      ['good.price']:e.detail.value
    })
  },
  add(e){
    let num = e.currentTarget.dataset.num
    num++
    this.setData({
      ['good.purchasNum']:num
    })
  },
  desc(e){
    let num = e.currentTarget.dataset.num
    if (num>0) {
      num--
    }
    this.setData({
      ['good.purchasNum']:num
    })
  },
  deletePrice(){
    this.setData({
      ['good.price']:0,
      salePrice:0
    })
  },
  //线下新增进货
  newAdd(){
    this.setData({
      isAdd:true
    })
  },
  closeAddBlock(){
    this.setData({
      isAdd:false
    })
    this.saveStock()
  },
  resetAdd(){
    this.setData({
      isAdd:false
    })
  },
  //库存盘点
  bindInventory(){
    this.setData({
      isInventory:true
    })
  },
  cancelInventory(){
    this.setData({
      isInventory:false
    })
  },
  remarkInventory(e){
    this.setData({
      inventoryNote:e.detail.value
    })
  },
  getAfterStock(e){
    this.setData({
      afterStock:e.detail.value
    })
  },
  confirmAfterStock(){
    let data = {
      skuId:this.data.good.skuId,
      changeForward:this.data.good.stock,
      changeBack:this.data.afterStock,
      optionId:this.data.accountId,
      note:this.data.inventoryNote
    }
    console.log(data)
    app.request('wx_app/agent/stock/skuStockRecord/save',data,'GET').then(res=>{
      console.log(res)
      if (res.code==1) {
        this.setData({
          isInventory:false
        })
        wx.showToast({
          title:'修改成功',
          icon:'success'
        })
        this.getGoodInfo(this.data.agentId)
      }else{
        app.errorMsg(res.message)
      }
    })
  },
  //盘点记录
  bindInventoryRecord(){
    wx.navigateTo({
      url:'../inventoryRecord/inventoryRecord?id='+this.data.good.skuId
    })
  },
  bindSetPrice(){
    this.setData({
      isSetPrice:true
    })
  },
  resetSet(){
    this.setData({
      isSetPrice:false
    })
  },
  getSalePrice(e){
    this.setData({
      ['good.price']:e.detail.value
    })
  }
})